package org.wlw.test;

import org.wlw.dao.IUserDao;
import org.wlw.dao.impl.UserDaoImpl;
import org.wlw.factory.DaoFactory;
import org.wlw.models.User;
import org.wlw.models.UserException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class UserUI {
	private IUserDao userDao;

	public UserUI() {
		userDao = DaoFactory.getUserDao();
	}

	private void add(BufferedReader br) throws IOException {
		System.out.println("请输入用户名:");
		String username = br.readLine();
		System.out.println("请输入密码:");
		String password = br.readLine();
		System.out.println("请输入用户昵称:");
		String nickname = br.readLine();
		System.out.println("请输入年龄:");
		//把字符串转换为整数，需要使用Integer.parseInt("xx")
		int age = Integer.parseInt(br.readLine());
		User user = new User();
		user.setUsername(username);
		user.setAge(age);
		user.setNickname(nickname);
		user.setPassword(password);
		userDao.add(user);
		System.out.println("添加用户成功!");
	}

	private void list() {
        final List<User> users = userDao.find();
        for(User u:users) {
            System.out.println(u);
        }
	}

	private void delete(BufferedReader br) throws IOException {
		System.out.println("输入要删除的用户名:");
		String username = br.readLine();
		userDao.deleteByUsername(username);
		System.out.println("删除用户成功!");
	}

	private void update(BufferedReader br) throws IOException {
		System.out.println("输入要更新的用户名:");
		String username = br.readLine();
		System.out.println("输入要更新的用户昵称:");
		String nickname = br.readLine();
		System.out.println("输入要更新的用户密码:");
		String password = br.readLine();
		System.out.println("输入要更新的用户年龄");
		int age = Integer.parseInt(br.readLine());
		User user = new User();
		user.setAge(age);
		user.setNickname(nickname);
		user.setPassword(password);
		user.setUsername(username);
		userDao.update(user);
		System.out.println("更新用户成功!");
	}

	private void load(BufferedReader br) throws IOException {
		System.out.println("请输入要查询的用户名:");
		String username = br.readLine();
		User user = userDao.loadByUsername(username);
		if(user==null) {
			System.out.println("查询的用户不存在!");
		} else {
			System.out.println(user);
		}
	}

	private void login(BufferedReader br) throws IOException {
		System.out.println("请输入登录的用户名:");
		String username = br.readLine();
		System.out.println("请输入登录的密码:");
		String password = br.readLine();
		User user = userDao.login(username, password);
		System.out.println("欢迎"+user.getNickname()+"登录我们的系统!呼呼呼！");
	}

	public void start() {
		BufferedReader br = null;
		show();
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			String str = null;
			while((str=br.readLine())!=null) {
				try {
					if(str.equalsIgnoreCase("A")) {
						add(br);
					} else if(str.equalsIgnoreCase("D")) {
						delete(br);
					} else if(str.equalsIgnoreCase("U")) {
						update(br);
					} else if(str.equalsIgnoreCase("F")) {
						list();
					} else if(str.equalsIgnoreCase("R")) {
						load(br);
					} else if(str.equalsIgnoreCase("L")) {
						login(br);
					} else if(str.equalsIgnoreCase("Q")) {
						System.out.println("欢迎下次继续使用我们的系统!");
						System.out.println("Bye!");
						break;
					} else {
						System.err.println("请选择一个正确的操作！");
					}
				} catch (UserException e) {
					System.err.println(e.getMessage());
				} catch(NumberFormatException e) {
					System.err.println("输入的格式不正确!请检查");
				}
				show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(br!=null) br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void show() {
		System.out.println("A(a):添加用户\tD(d):删除用户\tU(u):更新用户\t"
				+ "\tF(f):用户列表\tR(r):加载用户\tL(l):登录\tQ(q):退出系统");
		System.out.println("---------------------------------请选择相应的操作:---------------------------------");

	}
}
